package com.hanson.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.Const;
import com.hanson.common.exception.BusinessException;
import com.hanson.common.service.RedisService;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.CouponsBean;
import com.hanson.entity.CouponsSeqBean;
import com.hanson.entity.UserBean;
import com.hanson.mapper.CouponsBeanMapper;
import com.hanson.mapper.CouponsSeqBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CouponsService {
    @Autowired
    private CouponsBeanMapper couponsBeanMapper;
    @Autowired
    private CouponsSeqBeanMapper couponsSeqBeanMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    /**
     * 新增优惠卷
     *
     * @param couponsBean
     */
    public void addCoupons(CouponsBean couponsBean) {
        couponsBean.setCreateTime(new Date());
        couponsBeanMapper.insert(couponsBean);
    }

    /**
     * 修改优惠卷
     *
     * @param couponsBean
     */
    public void modifyCoupons(CouponsBean couponsBean) {
        CouponsBean returnBean = couponsBeanMapper.selectByPrimaryKey(couponsBean.getId());
        if (null != returnBean) {
            couponsBean.setCreateTime(returnBean.getCreateTime());
            couponsBeanMapper.updateByPrimaryKey(couponsBean);
        }
    }

    /**
     * 删除优惠卷
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCoupons(Integer id) {
        couponsBeanMapper.deleteByPrimaryKey(id);
        couponsSeqBeanMapper.deleteByCouponsId(id);
    }

    /**
     * 列表查询
     *
     * @param page
     * @param couponsBean
     * @return
     */
    public Page<CouponsBean> listCoupons(Page<CouponsBean> page, CouponsBean couponsBean) {
        return page.setRecords(couponsBeanMapper.listCoupons(page, couponsBean));
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    public CouponsBean detail(String id) {
        return couponsBeanMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 优惠卷列表
     *
     * @param openId
     * @return
     */
    public List<CouponsBean> getAllCoupons(String openId) {
        List<CouponsBean> list = couponsBeanMapper.selectAll();
        if (StringUtils.isEmpty(openId)) {
            return list;
        } else {
            if (CollectionUtils.isEmpty(list)) {
                return list;
            } else {
                UserBean userBean = userService.selectByOpenId(openId);
                if (null == userBean) {
                    throw new BusinessException("查询用户失败");
                } else {
                    List<CouponsBean> returnList = new ArrayList<>();
                    for (CouponsBean couponsBean : list) {
                        List<CouponsSeqBean> seqList = couponsSeqBeanMapper.selectByUserIdAndCouponsId(userBean.getId(), couponsBean.getId());
                        if (CollectionUtils.isEmpty(seqList)) {
                            returnList.add(couponsBean);
                        }
                    }
                    return returnList;

                }
            }
        }
    }

    /**
     * 领取优惠卷
     *
     * @param openId
     */
    public void receiveCoupons(String openId, String couponsId) {
        UserBean userBean = userService.selectByOpenId(openId);
        if (null == userBean) {
            throw new BusinessException("查询用户失败");
        }
        List<CouponsSeqBean> seqList = couponsSeqBeanMapper.selectByUserIdAndCouponsId(userBean.getId(), Integer.parseInt(couponsId));
        if (CollectionUtils.isNotEmpty(seqList)) {
            throw new BusinessException("当前优惠卷已被领取！");
        }
        CouponsSeqBean couponsSeqBean = new CouponsSeqBean();
        couponsSeqBean.setCouponsId(Integer.parseInt(couponsId));
        couponsSeqBean.setUserId(userBean.getId());
        couponsSeqBean.setType(1);
        couponsSeqBean.setCreateTime(new Date());
        couponsSeqBeanMapper.insert(couponsSeqBean);
        redisService.del(String.format(Const.USER_REDIS_KEY, openId));
    }

    /**
     * 查询一个用户可用优惠卷
     *
     * @param openId
     * @param totalPrice
     * @return
     */
    public List<CouponsBean> getAvailableCoupons(String openId, String totalPrice) {
        List<CouponsBean> couponsBeanList = getUserCoupons(openId, 1);
        if (CollectionUtils.isEmpty(couponsBeanList)) {
            return null;
        }
        List<CouponsBean> returnList = new ArrayList<>();
        for (CouponsBean couponsBean : couponsBeanList) {
            if (BusinessUtil.getDbAmount(totalPrice) >= BusinessUtil.getDbAmount(couponsBean.getMinAmount())) {
                returnList.add(couponsBean);
            }
        }
        return returnList;
    }

    /**
     * 查询一个用户所有的优惠卷
     * type: 1可用 2过期或已使用
     *
     * @param openId
     * @return
     */
    public List<CouponsBean> getUserCoupons(String openId, int type) {
        UserBean userBean = userService.selectByOpenId(openId);
        if (null == userBean) {
            throw new BusinessException("查询用户失败");
        }
        List<CouponsSeqBean> couponsSeqBeanList = couponsSeqBeanMapper.selectByUserIdAndType(userBean.getId(), type);
        if (CollectionUtils.isEmpty(couponsSeqBeanList)) {
            return null;
        } else {
            List<CouponsBean> list = new ArrayList<>();
            for (CouponsSeqBean couponsSeqBean : couponsSeqBeanList) {
                CouponsBean couponsBean = couponsBeanMapper.selectByPrimaryKey(couponsSeqBean.getCouponsId());
                list.add(couponsBean);
            }
            return list;
        }
    }
}
